package com.echo.im.server.netty.processor.impl;


import com.echo.im.common.enums.IMCmdType;
import com.echo.im.common.model.IMACKPrivateMessageInfo;
import com.echo.im.server.ack.MessageAckManager;
import com.echo.im.server.netty.processor.AbstractMessageProcessor;
import io.netty.channel.ChannelHandlerContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * ack处理器：用于处理对端对私聊消息的ack，群聊消息就不采用这种模式了
 * @author echo
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class AckPrivateMessageProcessor extends AbstractMessageProcessor<IMACKPrivateMessageInfo> {

    private final MessageAckManager messageAckManager;

    @Override
    public void process(ChannelHandlerContext ctx, IMACKPrivateMessageInfo ackInfo) {

        Long msgId = ackInfo.getMsgId();
        //加上用户信息也可以防止误删别人的msgId，但是真的感觉灭必要，增加复杂度
//        AttributeKey<Long> userIdAttr = AttributeKey.valueOf(ChannelAttrKey.USER_ID);
//        Long userId = ctx.channel().attr(userIdAttr).get();
        messageAckManager.confirmMessageAck(msgId);
        //还需不需要回对端一个确认包呢 ，我觉得也真的没有必要
    }

    @Override
    public void process(IMACKPrivateMessageInfo data) {

    }

    @Override
    public IMCmdType getCmdType() {
        return IMCmdType.ACK_PRIVATE_MESSAGE;
    }

    @Override
    public IMACKPrivateMessageInfo transForm(Object o) {
        return (IMACKPrivateMessageInfo) o;
    }
}
